#!/bin/bash

LOGF=/tmp/nklas-v4_u5-x86_64-hjb-patches.log 
BASEDIR=$(cd `dirname $0`; pwd)
date >> $LOGF

if grep -w aufs /proc/mounts >/dev/null; then
	echo -e '\033[31;5mMust run the script without power-down-protection!\033[0m'
	exit 1
fi

if [ $UID -ne 0 ]; then
	echo -e '\033[31;5mMust run the script as root!\033[0m'
	exit 2
fi

KV1='2.6.32-431.56.1.el6.1.x86_64'
KV2='2.6.32-431.11.2.el6.x86_64'
CKV=`uname -r`
if [ "$KV1" != "$CKV" -a "$KV2" != "$CKV"  ]; then
        echo -e '\033[31;5mThis kernel is not supported!\033[0m'
        exit 3
fi


force=
if [ "$1" = "--force" ]; then
	force="--force --nodeps"
fi


# update sm750 driver
echo update sm750 driver...
install -m 744 $BASEDIR/pkgs/sm750fb.ko   /lib/modules/`uname -r`/kernel/drivers/video
# update e1000e driver
install -m 744 $BASEDIR/pkgs/e1000e.ko /lib/modules/`uname -r`/kernel/drivers/net/e1000e/
# update igb driver
cp /lib/modules/`uname -r`/kernel/drivers/net/igb/igb.ko /var/log/igb.ko.orig -f
install -m 744 $BASEDIR/pkgs/igb.ko /lib/modules/`uname -r`/kernel/drivers/net/igb/

depmod


# rebuild initrd for net driver
if grep -w aufs /boot/grub/grub.conf >/dev/null; then
	aufs_arg="-I /sbin/mkfs.ext4"
else
	aufs_arg=
fi
echo "Rebuilding initramfs..."
dracut -f  $aufs_arg

if [ ! -e /var/log/grub.conf.orig ]; then
        cp /boot/grub/grub.conf /var/log/grub.conf.orig
fi

if ! grep 'video=sm750fb' /boot/grub/grub.conf  >/dev/null; then
        echo update grub
	sed '/root=/s/$/ video=sm750fb:1280x1024-32@60/;/System recovery/,/kernel/s/ video=sm750fb:.*$//' \
		/boot/grub/grub.conf -i
fi




user_in_home=

#disable sleep
rpm -Uvh $BASEDIR/pkgs/disable-sleep/*.rpm  $force
for u in $(ls /home); do
	if  id "$u" >/dev/null 2>&1; then
		if [ "$u" != "xguest" ]; then
			user_in_home="$u"
		fi
		su - "$u" -c \
		"gconftool-2 --set --type=int /apps/gnome-power-manager/timeout/sleep_computer_ac 0" > \
		/dev/null 2>>$LOGF
	fi
done

su - root -c \
"gconftool-2 --set --type=int /apps/gnome-power-manager/timeout/sleep_computer_ac 0" > \
/dev/null 2>>$LOGF

#set default value for new user
gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.system  \
--type int --set /apps/gnome-power-manager/timeout/sleep_computer_ac 0

#set igmp version to 2, set rp_filter to 0
if [ ! -e /var/log/sysctl.conf.orig ] >/dev/null; then
	cp /etc/sysctl.conf /var/log/sysctl.conf.orig -f
	sysctl -a |grep "force_igmp_version" | sed 's/= 0/= 2/' >> /etc/sysctl.conf
	sysctl -a |grep "\.rp_filter" | sed 's/= 1/= 0/' >> /etc/sysctl.conf
fi


# update productinfo
if ! grep XTY-07 /etc/.productinfo >/dev/null ; then
	echo '-XTY-07/20190807' >>  /etc/.productinfo
fi


# mount DEV_DATA to /home/<user>/data
DEV_DATA=/dev/sda5
if grep '\s/data\s' /etc/fstab >/dev/null && \
	[ -n "$user_in_home" ] && \
	mount |grep "$DEV_DATA" |grep "\s/data\s" >/dev/null; then
        umount  -f /data/
	e2label $DEV_DATA data
	mv /data /home/$user_in_home/
	mount $DEV_DATA  /home/$user_in_home/data
	chown $user_in_home /home/$user_in_home/data
	umount -f $DEV_DATA
        sed -i '/\/data/s/\/data/\/home\/'"$user_in_home"'&/' /etc/fstab
fi

#disable cancel button of system-backup-restore
mount -oremount,rw /backup
rpm -Uvh $force $BASEDIR/pkgs/zenity-2.28.0-1.el6.1.x86_64.rpm --root=/backup/
if ! grep no-cancel /backup/usr/sbin/backup-restore > /dev/null; then
	sed -r 's/^\s+zenity --progress/& --no-cancel/' /backup/usr/sbin/backup-restore -i 
fi
mount -oremount,ro /backup

#update sysctl.conf for net performance
if [ ! -e /var/log/.sysctl.conf.orig ]; then
	cp -af /etc/sysctl.conf /var/log/.sysctl.conf.orig
fi
if ! grep 'net.core.rmem_max = 83886080'  /etc/sysctl.conf > /dev/null; then
	echo 'net.core.netdev_max_backlog = 100000
net.core.rmem_max = 83886080
net.core.rmem_default = 83886080' >> /etc/sysctl.conf
fi
sed '/net.core.wmem_/d' /etc/sysctl.conf -i;

#erase icinga
yum erase cobra-api icinga icinga-idoutils -y

echo -e '\033[31;5mYou need to reboot the sytem!\033[0m'
